/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package mblog.web.controller.admin;

import mblog.core.persist.service.MailService;
import mblog.web.controller.BaseController;
import mtons.modules.pojos.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @author langhsu
 */
@Controller("mng_mail_ctl")
@RequestMapping("/admin/mail")
public class MailController extends BaseController {
    @Autowired
    private MailService mailService;

    @RequestMapping("/list")
    public String list(Integer pn, ModelMap model, HttpServletRequest request) {
        Paging page = wrapPage(pn);
        mailService.paging(page);
        model.put("page", page);
        return "/admin/mail/list";
    }


    @RequestMapping("/edit")
    public String edit() {
        return "/admin/mail/edit";
    }


    @RequestMapping("/import")
    public String importMail(String mails) {
        mailService.save(mails.split("\n"));
        return "redirect:/admin/mail/list";
    }


    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public String send(Integer sendAmount, String receiveMail, String subject, String content) {
        mailService.sendMail(sendAmount, receiveMail, subject, content);
        return "redirect:/admin/mail/list";
    }


}
